<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择种植户类型" clearable size="small">
          <el-option
            v-for="dict in famertypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="种植户" prop="farmerid">
        <el-select v-model="queryParams.farmerid" placeholder="请选择种植户" filterable clearable size="small">
          <el-option
            v-for="item in farmers"
            :key="item.userid"
            :label="item.name"
            :value="item.userid"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="批次" prop="orderbatchguid">
        <el-select v-model="queryParams.orderbatchguid" placeholder="请选择批次编号" filterable clearable size="small">
          <el-option
            v-for="item in famerbatchOptions"
            :key="item.guid"
            :label="item.name"
            :value="item.guid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="负责人" prop="orderownername">
        <el-select v-model="queryParams.orderownername" placeholder="请选择负责人" filterable clearable size="small">
          <el-option
            v-for="dict in userOptions"
            :key="dict.guid"
            :label="dict.realname"
            :value="dict.guid"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['farmer:farmerorder:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['farmer:farmerorder:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['farmer:farmerorder:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['farmer:farmerorder:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="farmerorderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="种植户" align="center" prop="farmer" width="130"/>
      <el-table-column label="类型" align="center" prop="typename"/>
      <el-table-column label="电话" align="center" prop="telphone" width="100"/>
      <el-table-column label="自有仓库" align="center" prop="storecapcity"/>
      <el-table-column label="订单年份" align="center" prop="yearoforder"/>
      <el-table-column label="批次" align="center" prop="batchname"/>
      <el-table-column label="种植面积" align="center" prop="orderarea"/>
      <el-table-column label="种植品种" align="center" prop="productname" width="120"/>
      <el-table-column label="订单负责人" align="center" prop="orderownername"/>
      <el-table-column label="种薯数量" align="center" prop="seedweight"/>
      <el-table-column label="农药数量" align="center" prop="pestweight"/>
      <el-table-column label="化肥数量" align="center" prop="fertweight"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['farmer:farmerorder:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['farmer:farmerorder:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules"  :inline="true" label-width="120px">
        <el-form-item label="订单名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入订单名称"/>
        </el-form-item>
        <el-form-item label="所属部门" prop="departmentguid">
          <el-select v-model="form.departmentguid" placeholder="请选择所属部门" filterable clearable size="small">
            <el-option
              v-for="item in departmentsADD"
              :key="item.guid"
              :label="item.name"
              :value="item.guid"
              @click.native="getFarmersAdd($event, item)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="种植户" prop="farmerid">
          <el-select v-model="form.farmerid" placeholder="请选择种植户" filterable clearable size="small">
            <el-option
              v-for="item in farmersAdd"
              :key="item.userid"
              :label="item.name"
              :value="item.userid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="种植户类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择种植户类型" clearable size="small">
            <el-option
              v-for="dict in famertypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="订单年份" prop="yearoforder">
          <el-select v-model="form.yearoforder" placeholder="请选择订单年份" clearable size="small">
            <el-option
              v-for="dict in years"
              :key="dict"
              :label="dict"
              :value="dict"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="批次编号" prop="orderbatchguid">
          <el-select v-model="form.orderbatchguid" placeholder="请输入批次编号" filterable clearable size="small">
            <el-option
              v-for="item in famerbatchOptionsADD"
              :key="item.guid"
              :label="item.name"
              :value="item.guid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="种植面积(亩)" prop="orderarea">
          <el-input v-model="form.orderarea" placeholder="请输入种植面积"/>
        </el-form-item>
        <el-form-item label="种植品种" prop="productname">
          <el-input v-model="form.productname" placeholder="请输入种植品种"/>
        </el-form-item>
        <el-form-item label="种薯销售数(吨)" prop="seedweight">
          <el-input v-model="form.seedweight" placeholder="请输入种薯销售数量"/>
        </el-form-item>
        <el-form-item label="农药销售数(吨)" prop="pestweight">
          <el-input v-model="form.pestweight" placeholder="请输入农药销售数量"/>
        </el-form-item>
        <el-form-item label="化肥销售数(吨)" prop="fertweight">
          <el-input v-model="form.fertweight" placeholder="请输入化肥销售数量"/>
        </el-form-item>
        <el-form-item label="订单签订负责人" prop="orderownerid">
          <el-select v-model="form.orderownerid" placeholder="请输入订单签订负责人" clearable filterable size="small">
            <el-option
              v-for="dict in userOptions"
              :key="dict.guid"
              :label="dict.realname"
              :value="dict.guid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="种植过程负责人" prop="plantownerid">
          <el-select v-model="form.plantownerid" placeholder="请输入种植过程负责人" clearable filterable size="small">
          <el-option
            v-for="dict in userOptions"
            :key="dict.guid"
            :label="dict.realname"
            :value="dict.guid"
          />
          </el-select>
        </el-form-item>
        <el-form-item label="订单状态">
          <el-select v-model="form.status" placeholder="请选择订单状态" clearable size="small">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listFarmerorder,
    exportFarmerorder,
    addFarmerorder,
    delFarmerorder,
    updateFarmerorder,
    getFarmerorder
  } from "@/api/farmer/farmerorder";
  import {getHasPermissionFarmers, getAllFarmers} from "@/api/farmer/farmerinfo";
  import {getAll} from "@/api/farmer/farmerbatch";
  import {tuserList, findDepartment} from "@/api/public";
  import {getPermisForDepart} from "@/api/organization/department";

  export default {
    name: "Farmerorder",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 种植订单表格数据
        farmerorderList: [],
        statusOptions: [],
        years: [],
        famertypeOptions: [],
        famerbatchOptions: [],
        famerbatchOptionsADD: [],
        farmers: [],
        farmersAdd: [],
        userOptions: [],
        departments: [],
        departmentsADD: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          farmerid: null,
          orderbatchguid: null,
          type: null,
          orderownername: null,
          status: null,
          functionTypes: 24,
          departype: 3,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          lastupdate: [
            {required: true, message: "最后更新时间不能为空", trigger: "blur"}
          ]
        }
      };
    },
    created() {
      this.getList();
      this.tuserList();
      this.getFarmers();
      this.getAll();
      this.getDeparts();
      this.generateYear();
      this.findDepartment();
      this.getDicts("famerbatch_status").then(response => {
        this.statusOptions = response.data;
      });
      this.getDicts("famer_type").then(response => {
        this.famertypeOptions = response.data;
      });

    },
    methods: {
      getFarmersAdd(event, item) {
        this.getFarmersAll(item.guid);
        getAll({deptguid: item.guid}).then(response => {
          this.famerbatchOptionsADD = response.data;
        });
      },
      generateYear() {
        let date = new Date();
        let years = [];
        for (let i=1; i<11; i++) {
          years.push(date.getFullYear() - 5 + i);
        }
        this.years = years;
      },
      getDeparts() {
        getPermisForDepart(this.queryParams).then(response => {
          this.departments = response.data;
        });
      },
      findDepartment() {
        findDepartment({permission:41}).then(response => {
          this.departmentsADD = response.data;
        });
      },
      /** 查询种植订单列表 */
      getList() {
        this.loading = true;
        listFarmerorder(this.queryParams).then(response => {
          this.farmerorderList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      getAll() {
        getAll().then(response => {
          this.famerbatchOptions = response.data;
        });
      },
      tuserList() {
        tuserList({usertype : 2}).then(response => {
          this.userOptions = response.data;
        });
      },
      getFarmers() {
        getHasPermissionFarmers().then(response => {
          this.farmers = response.data;
        });
      },
      getFarmersAll(departid) {
        getAllFarmers({deptguid: departid, status: 1}).then(response => {
          this.farmersAdd = response.data;
        });
      },
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          guid: null,
          farmerid: null,
          orderbatchguid: null,
          departmentguid: null,
          yearoforder: null,
          name: null,
          orderarea: null,
          type: null,
          productname: null,
          seedweight: null,
          pestweight: null,
          fertweight: null,
          orderownerid: null,
          orderownername: null,
          plantownerid: null,
          plantownername: null,
          status: null,
          operatorid: null,
          createtime: null,
          code: null,
          lastupdate: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.guid)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加种植订单";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const guid = row.guid || this.ids
        getFarmerorder(guid).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改种植订单";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.guid != null) {
              updateFarmerorder(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addFarmerorder(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const guids = row.guid || this.ids;
        this.$confirm('是否确认删除该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delFarmerorder(guids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有种植订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportFarmerorder(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
